<template>
  <el-form label-position="top">
    <el-form-item 
      v-for="(event, key) in component.events" 
      :key="key" 
      :label="event.label || key"
    >
      <el-switch
        v-model="componentEventsModel[key]"
        :active-value="true"
        :inactive-value="false"
        @change="(val) => val && handleEvent(key)"
      />
      <div class="event-description" v-if="event.description">
        {{ event.description }}
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
import { defineComponent, computed } from 'vue'

export default defineComponent({
  name: 'EventsConfig',
  props: {
    component: {
      type: Object,
      required: true
    },
    componentEvents: {
      type: Object,
      required: true
    }
  },
  emits: ['update:componentEvents', 'handleEvent'],
  setup(props, { emit }) {
    // 使用计算属性创建双向绑定
    const componentEventsModel = computed({
      get: () => props.componentEvents,
      set: (value) => emit('update:componentEvents', value)
    })

    // 处理事件触发
    const handleEvent = (eventName) => {
      emit('handleEvent', eventName)
    }

    return {
      componentEventsModel,
      handleEvent
    }
  }
})
</script>

<style scoped>
.event-description {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}
</style>